package gui;

import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JScrollBar;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

import java.awt.Color;
import javax.swing.AbstractListModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controllers.LibrarianUIP;
import controllers.OBLSettings;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * This class representing a Librarian Gui Add/Edit Copy
 * Date: 29/12/2012
 * @author Tali
 *
 */



public class LibrarianAddEditCopyUI extends JPanel 
{
	
	    private static final long serialVersionUID = 1L;
	    
	    private LibrarianUIP luip=null;
		JTextField txtCopyID;
		JTextField txtISBN;
	    JTextField txtPurchaseDate;
		JLabel lblCopyID;
		JLabel lblISBN;
		JLabel lblPurchsaDate;
		private JSeparator separator;
		JLabel lblTitleLabel;
		JButton btnExitButton;
	    JButton btnLogoutButton;
	    JButton btnCopyOperation;
	    JButton btnBack;
	    LibrarianUI librarianPanel=null;
	    JTextField txtBorrow;
	    private JLabel lblBorrow;
	    private JLabel backgroundlabel;

//===============================================================================================
//		 Constructor
//		 
/**
* Constructor the LibrarianAddEditCopyUI with default values
* @param librarianUI
*/

	    public LibrarianAddEditCopyUI(LibrarianUI librarianUI) 
	    {
	    	super();
			this.librarianPanel=librarianUI;
			this.luip=new LibrarianUIP();
			initialize();	
		}

		private void initialize()
		{
		   this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
			setLayout(null);
			
			
			lblBorrow = new JLabel("Borrow :");
			lblBorrow.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblBorrow.setBounds(35, 401, 174, 24);
			add(lblBorrow);
			
			txtCopyID = new JTextField();
			txtCopyID.setFont(new Font("Tahoma", Font.PLAIN, 13));
			txtCopyID.setBounds(266, 163, 265, 34);
			add(txtCopyID);
			txtCopyID.setColumns(10);
			
			txtISBN = new JTextField();
			txtISBN.setFont(new Font("Tahoma", Font.PLAIN, 13));
			txtISBN.setBounds(266, 236, 265, 34);
			add(txtISBN);
			txtISBN.setColumns(10);
			
			txtPurchaseDate = new JTextField();
			txtPurchaseDate.setFont(new Font("Tahoma", Font.PLAIN, 13));
			txtPurchaseDate.setBounds(266, 320, 265, 34);
			add(txtPurchaseDate);
			txtPurchaseDate.setColumns(10);
			
			lblCopyID = new JLabel("Copy Book Number :");
			lblCopyID.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblCopyID.setBounds(35, 163, 142, 31);
			add(lblCopyID);
			
			lblISBN = new JLabel("ISBN :");
			lblISBN.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblISBN.setBounds(35, 239, 174, 24);
			add(lblISBN);
			
			lblPurchsaDate = new JLabel("Purchase Date :");
			lblPurchsaDate.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lblPurchsaDate.setBounds(35, 323, 174, 24);
			add(lblPurchsaDate);
			
			separator = new JSeparator();
			separator.setBounds(0, 57, 881, 24);
			add(separator);
			
			lblTitleLabel = new JLabel("OBL   Add New Copy");
			lblTitleLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
			lblTitleLabel.setBounds(267, -1, 247, 60);
			add(lblTitleLabel);
			
			btnLogoutButton = new JButton("Logout");
			btnLogoutButton.setFont(new Font("Tahoma", Font.BOLD, 15));
			btnLogoutButton.setBounds(603, 11, 83, 41);
			add(btnLogoutButton);
			
			btnExitButton = new JButton("Exit");
		
			btnExitButton.setFont(new Font("Tahoma", Font.BOLD, 15));
			btnExitButton.setBounds(696, 11, 83, 41);
			add(btnExitButton);
			
			if (librarianPanel.op_Copy.equals("AddCopy")) 
				btnCopyOperation = new JButton("Add Copy");
			else
				btnCopyOperation = new JButton("Edit Copy");
			
			btnCopyOperation.setFont(new Font("Tahoma", Font.PLAIN, 15));
			btnCopyOperation.setBounds(321, 505, 142, 41);
			add(btnCopyOperation);
	
			
			btnBack = new JButton("Back");
			btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
			btnBack.setBounds(10, 11, 83, 41);
			add(btnBack);
			
			txtBorrow = new JTextField();
			txtBorrow.setFont(new Font("Tahoma", Font.PLAIN, 13));
			txtBorrow.setColumns(10);
			txtBorrow.setBounds(266, 398, 265, 34);
			add(txtBorrow);
			
			backgroundlabel = new JLabel("");
			backgroundlabel.setIcon(OBLSettings.IMG_LIBRARIAN_USER_BOOK_COPY);
			backgroundlabel.setBounds(0, 10, 800, 600);
			add(backgroundlabel);

			
// ===========================================================================================
//			 Action Listeners
//          
			//back button (back to librarian main panel)
			btnBack.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					WelcomeUI.wui.switchPanel("OBL  - Librarian", librarianPanel, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
					txtBorrow.setText(null);
					txtCopyID.setText(null);
					txtISBN.setText(null);
					txtPurchaseDate.setText(null);
					librarianPanel.searchResultTableCopy.model.setRowCount(0);
				}
			});
//========================================================================================
			
			btnCopyOperation.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e) 
				{
					if (librarianPanel.op_Copy.equals("AddCopy"))
					{
						int createCopyRes=luip.AddCopy(getCopyID(),getCopyOfISBN(),getPurchaseDate(),getBorrow());	
						if (!getCopyID().isEmpty() &&  !getCopyOfISBN().isEmpty() && !getPurchaseDate().isEmpty() && !getBorrow().isEmpty())  
						{
							switch (createCopyRes)
							{
								
								case 1: // Book created successfully
								{
									txtCopyID.setText(null);
									txtISBN.setText(null);
									txtPurchaseDate.setText(null);
									txtBorrow.setText(null);
						
						
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Copy created successfully!","Create Copy",JOptionPane.INFORMATION_MESSAGE);
									break;
								} 
								case -1: // Copy already exists
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Copy already exists!","Warning",JOptionPane.WARNING_MESSAGE);
									break;
								}
								default: // Error
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
									break;
								}
							} // End switch			
						}
						else //if
							JOptionPane.showMessageDialog( WelcomeUI.wui, "Not all fields are filled!","Warning",JOptionPane.WARNING_MESSAGE);
					
					} // AddCopy
					else 
					{  //Edit
						
						String copyID = getCopyID();
						txtISBN.enable();
						txtBorrow.enable();
						txtCopyID.enable();
						int editCopyRes=luip.editCopy(copyID,getPurchaseDate());
						System.out.println(editCopyRes);
						if (!getCopyID().isEmpty() &&  !getCopyOfISBN().isEmpty() && !getPurchaseDate().isEmpty() && !getBorrow().isEmpty()) 
						{
							switch (editCopyRes)
							{
								case 1: // Copy update successfully
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, " Edit succeded ","Done",JOptionPane.INFORMATION_MESSAGE);
			                        break;								
								}
								case -1: // Error
								{
								JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
								break;
								}
								default: // Error
								{
									JOptionPane.showMessageDialog( WelcomeUI.wui, "Operation failed!","Warning",JOptionPane.ERROR_MESSAGE);				
									break;
								}
							}//switch
						}//if
					else //if
						JOptionPane.showMessageDialog( WelcomeUI.wui, "Not all fields are filled!","Warning",JOptionPane.WARNING_MESSAGE);

					}

				  }		
			
			});
		}
//======================================================================================================
		public String getCopyID() 
		{
			return this.txtCopyID.getText();
		}

		public void setCopyID(JTextField txtCopyID)
		{
			this.txtCopyID = txtCopyID;
		}

		public String getPurchaseDate() 
		{
			return this.txtPurchaseDate.getText();
		}

		public void setPurchaseDate(JTextField txtPurchaseDate) 
		{
			txtPurchaseDate = txtPurchaseDate;
		}

		public String getCopyOfISBN() 
		{
			return this.txtISBN.getText();
		}

		public void setCopyOfISBN(JTextField txtISBN) 
		{
			txtISBN = txtISBN;
		}
		
		public String getBorrow() 
		{
			return this.txtBorrow.getText();
		}

		public void setBorrow(JTextField txtBorrow) 
		{
			txtBorrow = txtBorrow;
		}
		
		public void paintComponent(Graphics g) 
		{
			g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
			
		}
	}
		
		

